//
//  FighterMoveByAction.cpp
//  airfight
//
//  Created by aaron more on 12-9-25.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include "FighterMoveByAction.h"

//
// FighterMoveByAction
//

FighterMoveByAction* FighterMoveByAction::create(Move* p_move)
{
    FighterMoveByAction *pMoveByAction = new FighterMoveByAction();
    float duration = p_move->getDuration();
    CCPoint position = p_move->getEndPoint();
    
    pMoveByAction->initWithDuration(duration, position);
    pMoveByAction->setMoveStep(p_move->getStep());
    pMoveByAction->setMoveAngle(p_move->getAngle());
    pMoveByAction->setAngleWish(p_move->getWish());
    pMoveByAction->autorelease();
    
    return pMoveByAction;
}

bool FighterMoveByAction::initWithDuration(float duration, const CCPoint& position)
{
    if (CCActionInterval::initWithDuration(duration))
    {
        m_delta = position;
        m_endPosition = position;
        return true;
    }
    
    return false;
}

void FighterMoveByAction::startWithTarget(CCNode *pTarget)
{
    CCActionInterval::startWithTarget(pTarget);
    m_startPosition = pTarget->getPosition();
    //m_delta = ccpSub(m_endPosition, m_startPosition);

}

void FighterMoveByAction::update(float time)
{
    
    if (m_pTarget)
    {
        EnemyFighterSprite* enemySprite = (EnemyFighterSprite*)m_pTarget;
        int fighterID = enemySprite->getFighterId();

        float movex = m_delta.x * time;
        float movey = m_delta.y * time;
        CCPoint tmp_point =  ccp(m_startPosition.x + movex,
                                 m_startPosition.y + movey);
//        char framename[1024] = {0};
//        if (movex > 0)
//        {
//            sprintf(framename, "enemy%d_left.png", fighterID);
//        }else if (movex < 0)
//        {
//            sprintf(framename, "enemy%d_right.png", fighterID);
//        }else
//        {
//            sprintf(framename, "enemy%d.png", fighterID);
//        }
//        
//        CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(framename);  
//        enemySprite->setDisplayFrame(frame); 
        if(this->getAngleWish() == 1){
            enemySprite->setRotation(this->getMoveAngle());
        }
        else
        {
            float32 angle = atan2(tmp_point.x - m_startPosition.x,tmp_point.y - m_startPosition.y);
            enemySprite->setRotation(angle/PI*180);
        }
        
        enemySprite->setPosition(tmp_point);
    }
}

